package db1;

import beans1.Doctorbean;

import java.sql.*;
import java.util.ArrayList;

public class DoctorDAO extends ConnBean{
    private Connection connection=null;
    public ArrayList<Doctorbean> getAllRecords(){
        ResultSet rs=null;
        Statement stmt=null;
        ArrayList<Doctorbean> list=new ArrayList<Doctorbean>();
        try {
            //获取数据库连接(url,username,password)
            connection = getConnection();
            if(connection==null)
                System.out.println("error");
            //准备执行数据库查询
            String sql="select * from YY_DOCTOR";
            //准备SQL执行;
            stmt =connection.createStatement();
            //执行数据库查询，获取结果集
            rs=stmt.executeQuery(sql);
            while(rs.next()){
            	Doctorbean user=new Doctorbean();
                user.setId(rs.getString("id"));
                user.setPassword(rs.getString("password"));
                user.setName(rs.getString("name"));
                user.setIdnumber(rs.getString("idnumber"));
                user.setSex(rs.getString("Sex"));
                user.setKsid(rs.getString("Ksid"));
                user.setPhoto(rs.getString("photo"));
                user.setIntroduction(rs.getString("introduction"));
                user.setType(rs.getString("type"));
                list.add(user);
            }
            //关闭表达式对象
            stmt.close();
            //关闭数据库
            connection.close();

        }catch (SQLException e) {
            e.printStackTrace();
        } finally{
            try{
                if (connection!=null && (!connection.isClosed())){
                    connection.close();
                }
            }catch(SQLException e){
                e.printStackTrace();
            }
        }
        return list;
    }


    public boolean deleteRecord(Doctorbean user) {
        PreparedStatement pstmt = null;
        String sql = "delete from YY_DOCTOR where ID=?";
        if (user == null)
            return false;
        int result = 0;
        try {
            connection = getConnection();
            pstmt = connection.prepareStatement(sql);
            pstmt.setString(1, user.getId());
            result = pstmt.executeUpdate();

            System.out.println("the id is"+user.getId());
            //关闭表达式对象
            pstmt.close();
            //关闭数据库
            connection.close();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (connection != null && (!connection.isClosed())) {
                    connection.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (result > 0)
            return true;
        else
            return false;
    }
    public boolean editDoctor(Doctorbean user) throws SQLException {
        connection = getConnection();
        String sql="update YY_DOCTOR set password=?,name=?,idNumber=?,sex=?,ksid=?,photo=?,INTRODUCTION=?,type=? where id=?";

        PreparedStatement pstmt=connection.prepareStatement(sql);
        //设置占位符值
        pstmt.setString(1, user.getPassword());
        pstmt.setString(2, user.getName());
        pstmt.setString(3, user.getIdnumber());
        pstmt.setString(4, user.getSex());
        pstmt.setString(5, user.getKsid());
        pstmt.setString(6, user.getPhoto());
        pstmt.setString(7, user.getIntroduction());
        pstmt.setString(8, user.getType());
        pstmt.setString(9, user.getId());

        //执行sql语句，返回影响行数
        int res=pstmt.executeUpdate();
        if(res>0){
            System.out.println("更新数据成功");
        }
        else
        {
            System.out.println("更新数据失败");
        }
        pstmt.close();
        connection.close();
        return true;
    }

    public boolean insertDoctor(Doctorbean user) throws SQLException {
        connection = getConnection();
        String sql = "INSERT INTO YY_DOCTOR (ID,PASSWORD,NAME,IDNUMBER,SEX,KSID,PHOTO,INTRODUCTION,TYPE) VALUES(?,?,?,?,?,?,?,?,?)";
        PreparedStatement pstmt=connection.prepareStatement(sql);

        pstmt.setString(1, user.getId());
        pstmt.setString(2, user.getPassword());
        pstmt.setString(3, user.getName());
        pstmt.setString(4, user.getIdnumber());
        pstmt.setString(5, user.getSex());
        pstmt.setString(6, user.getKsid() );
        pstmt.setString(7, user.getPhoto());
        pstmt.setString(8, user.getIntroduction() );
        pstmt.setString(9, user.getType());
        int res=pstmt.executeUpdate();
        if(res>0){
            System.out.println("成功加入数据");
        }
        pstmt.close();
        connection.close();
        return true;
    }
}
